import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_test1/extension/string_extension.dart';
import 'package:flutter_test1/gen/assets.gen.dart';
import 'package:flutter_test1/widgets/app_button.dart';
import 'package:flutter_test1/widgets/app_text_field.dart';
import 'package:get/get.dart';

class AppSearchBar extends StatelessWidget {
  /// 是否点击触发重定位
  final bool searchIconVisible;
  final Widget? prefix;
  final Widget? suffix;
  final TextEditingController? controller;
  final Color? backgroundColor;
  final String? hint;
  final TextStyle? hintStyle;
  final FocusNode? fs;
  final bool readOnly;
  final Function? onTap;
  final double? borderRadius;
  final TextAlign? textAlign;
  final Function? onSearch;

  const AppSearchBar({
    super.key,
    this.hint,
    this.searchIconVisible = true,
    this.prefix,
    this.suffix,
    this.controller,
    this.backgroundColor,
    this.fs,
    this.readOnly = false,
    this.onTap,
    this.hintStyle,
    this.borderRadius,
    this.textAlign,
    this.onSearch,
  });

  @override
  Widget build(BuildContext context) {

    return AppTextField(
      focusNode: fs,
      controller: controller,
      backgroundColor:
          backgroundColor ??  Color(0xFFF0F0F0),
      borderRadius: borderRadius ?? 90,
      hintText: hint ?? '',
      hintStyle: hintStyle ??
          TextStyle(
              fontSize: 14.sp,
              color: Theme.of(context).textTheme.titleSmall!.color),
      minHeight: 40.h,
      padding: EdgeInsets.only(left: 15.w),
      // padding: EdgeInsets.zero,
      prefix: searchIconVisible ? iconPrefix() : null,
      suffix: suffix ?? searchButton(),
      textAlign: textAlign ?? TextAlign.start,
      maxLength: 50,
      readOnly: readOnly,
      contentPadding: EdgeInsets.only(bottom: 5.h),
      onTap: () => onTap?.call(),
    );
  }

  Widget iconPrefix() {
    return Assets.images.iconSearch.image(
      width: 20.r,
      height: 20.r
    ).marginOnly(right: 10.w);
  }

  Widget searchButton() {
    return AppButton(
      width: 50.w,
      height: 30.h,
      onPressed: () => onSearch?.call(),
      content: '搜索',
      // child: '搜索'.extText(fontSize: 24.sp, color: Colors.white),
      animate: false,
      borderRadius: 12.r,
    ).marginOnly(right: 10.w);
  }
}
